home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / CONTRIB / xspeakfree-0.7 / lib / xspeakfree / newdialog.tcl < prev    next >
Text File  |  2000-05-18  |  3KB  |  135 lines

  1. # newdialog.tcl --
  2. #
  3. # newdialog
  4. #
  5. # This procedure displays a dialog box, waits for a button in the dialog
  6. # to be invoked, then returns the index of the selected button.  If the
  7. # dialog somehow gets destroyed, -1 is returned.
  8. #
  9. # Arguments:
  10. #    -path        -- path to use for window, default=.newdialog
  11. #    -title        -- title to use for window, default=Dialog
  12. #    -create        -- script to pack dialog
  13. #    -after        -- script to run after dialog has been closed, either through a destroy or a change of variable
  14. #    -variable    -- variable to watch for change
  15. #    -grab        -- boolean whether to take focus or not
  16.  
  17. proc newdialog {args} {
  18.     set w .newdialog
  19.     set title Dialog
  20.     set create {}
  21.     set after {}
  22.     set var {}
  23.     set grabFocus true 
  24.     foreach {n v} $args {
  25.         switch -exact -- $n {
  26.             -path {set w $v}
  27.             -title {set title $v}
  28.             -create {set create $v}
  29.             -after {set after $v}
  30.             -variable {set var $v}
  31.             -grab {set grabFocus $v}
  32.         }
  33.     }
  34.     if {$var == {}} {
  35.         error {-variable cannot be null, must be a global}
  36.         return
  37.     }
  38.     global $var
  39.  
  40.     catch {destroy $w}
  41.     toplevel $w
  42.     wm title $w $title
  43.     wm iconname $w $title
  44.     bind $w <Destroy> "set $var {<Destroyed>}"
  45.     wm protocol $w WM_DELETE_WINDOW "destroy $w"
  46.  
  47.     # The following command means that the dialog won't be posted if
  48.     # [winfo parent $w] is iconified, but it's really needed;  otherwise
  49.     # the dialog can become obscured by other windows in the application,
  50.     # even though its grab keeps the rest of the application from being used.
  51.  
  52.     wm transient $w [winfo toplevel [winfo parent $w]]
  53.  
  54.     eval $create
  55.  
  56.     wm withdraw $w
  57.     update idletasks
  58.     set wparent [winfo parent $w]
  59.     set x [expr [winfo width $wparent]/2 - [winfo reqwidth $w]/2 + [winfo x $wparent]]
  60.     set y [expr [winfo height $wparent]/2 - [winfo reqheight $w]/2 + [winfo y $wparent]]
  61.  
  62.     wm geom $w +$x+$y
  63.     wm deiconify $w
  64.  
  65.     if {$grabFocus} {
  66.         # Set a grab and claim the focus too.
  67.         set oldFocus [focus]
  68.         set oldGrab [grab current $w]
  69.         if {$oldGrab != ""} {
  70.             set grabStatus [grab status $oldGrab]
  71.         }
  72.         grab $w
  73.     }
  74.  
  75.     tkwait variable $var
  76.     eval $after
  77.  
  78.     if {$grabFocus} {
  79.         catch {focus $oldFocus}
  80.     }
  81.     catch {
  82.         # It's possible that the window has already been destroyed,
  83.         # hence this "catch".  Delete the Destroy handler so that
  84.         # $var doesn't get reset by it.
  85.  
  86.         bind $w <Destroy> {}
  87.         destroy $w
  88.     }
  89.     if {$grabFocus} {
  90.         if {$oldGrab != ""} {
  91.             if {$grabStatus == "global"} {
  92.                 grab -global $oldGrab
  93.             } else {
  94.                 grab $oldGrab
  95.             }
  96.         }
  97.     }
  98.  
  99.     return [eval "return $$var"]
  100. }
  101.  
  102. # newwindow
  103. # Arguments
  104. #    -path    -- path to use for window
  105. #    -title    -- title to use for window
  106. #    -delete    -- script to run when window is deleted by window manager
  107.  
  108. proc newwindow {args} {
  109.  
  110.     set w {}
  111.     set title {}
  112.     set del {}
  113.  
  114.     foreach {n v} $args {
  115.         switch -exact -- $n {
  116.             -path {set w $v}
  117.             -title {set title $v}
  118.             -delete {set del $v}
  119.         }
  120.     }
  121.     if {$w == {}} {
  122.         error {-path cannot be null}
  123.         return
  124.     }
  125.  
  126.     catch {destroy $w}
  127.     toplevel $w
  128.     wm title $w $title
  129.     wm iconname $w $title
  130.     if {$del != {}} {
  131.         wm protocol $w WM_DELETE_WINDOW $del
  132.     }
  133.     return $w
  134. }
  135.